import { Toast } from "vant";
export default {
  props: {
    applyType: {
      type: String | Number,
      required: true
    }
  },
  data() {
    return {
      formData: {
        type: '1',
        name: '',
        mobile: '',
        province_id: '',
        city_id: '',
        district_id: '',
        street_id: '',
        address: '',
        license: [],
        identity_1: '',
        identity_2: ''
      },
      addressName: "",
      addressShow: false,
      area: district,
      district: "",
      strShow: false,
      streetShow: false,
      districtVal: [],
      street: '',
      photosize: 1024 * 1024 * 5,
      //当前点击的图片上传索引，因为vant的上传图片参数name作为标识无效
      selectImgIndex: null,
      uploadUrl: this.fun.getRealUrl("upload.uploadPic", {})
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    /**
     * 初始化数据
     */
    init: function() {
      this.formData.name = '';
      this.formData.mobile = '';
      this.formData.province_id = '';
      this.formData.city_id = '';
      this.formData.district_id = '';
      this.formData.street_id = '';
      this.formData.address = '';
      this.formData.license = [];
      this.formData.identity_1 = '';
      this.formData.identity_2 = '';
    },

    typeChange: function() {
      this.init();
    },

    /**
     * 所在城市选中
     * @param obj 
     */
    addressCallback: function(obj) {
      this.addressName =
        obj.itemName1 + " " + obj.itemName2 + " " + obj.itemName3;
      this.formData.province_id = obj.itemValue1;
      this.formData.city_id = obj.itemValue2;
      this.formData.district_id = obj.itemValue3;
      this.getStreet(obj.itemValue3);
    },

    /**
     * 根据县级id获取辖区街道
     * @param district_id 县级id
     */
    getStreet: function(district_id) {
      $http
        .get("member.memberAddress.street", { district_id })
        .then(response => {
          if (response.result == 1) {
            if (
              !this.fun.isTextEmpty(response.data) &&
              response.data.length > 0
            ) {
              this.districtVal = response.data;
              this.strShow = true;
            } else {
              this.strShow = false;
            }
          } else {
            this.strShow = false;
          }
        })
        .catch(error => {
          console.log(error);
        });
    },

    onClickLeft(){
      this.streetShow = false;
    },

    streetConfirm(item) {
      this.street = item.areaname;
      this.formData.street_id = item.id;
      this.streetShow = false;
    },

    // 点击获取上传图片信息
    multipleMethod_1(e, flag) {
      if (e && e.length) {
        for (let i = 0; i < e.length; i++) {
          this.onReadMore(e[i], flag);
        }
      } else {
        this.onReadMore(e, flag);
      }
    },

    // vant 多图片上传
    onReadMore(e, flag) {
      Toast.loading({
        message: "上传中",
        forbidClick: true
      });
      let fd = new FormData();
      if (flag === true) {
        fd.append("file", e);
      } else {
        fd.append("file", e.file); // 第一个参数字符串可以填任意命名，第二个根据对象属性来找到file
      }
      var That = this;
      axios
        .post(this.fun.getRealUrl("upload.uploadPic"), fd, {
          headers: { "Content-Type": "multipart/form-data" }
        })
        .then(response => {
          var responseData = response.data;
          if (responseData.result === 1) {
            That.formData.license.push(responseData.data.img_url);
          } else {
            Toast(responseData.msg);
          }
        })
        .finally(error => {
          Toast.clear();
        });
    },

    // 删除图片
    removeImage: function(delIndex) {
      this.formData.license.splice(delIndex, 1);
    },

    selectIndex(i) {
      this.selectImgIndex = i;
    },

    /*vant图片上传*/
    onRead(e, flag) {
      Toast.loading({
        message: '上传中',
        forbidClick: true,
      });
      let fd = new FormData();

      if (flag === true) {
        fd.append("file", e); // 第一个参数字符串可以填任意命名，第二个根据对象属性来找到file
      } else {
        fd.append("file", e.file); // 第一个参数字符串可以填任意命名，第二个根据对象属性来找到file
      }
      var That = this;
      axios
        .post(this.uploadUrl, fd, {
          headers: { "Content-Type": "multipart/form-data" }
        })
        .then(response => {
          var responseData = response.data;
          if (responseData.result === 1) {
            Toast.clear();

            switch (That.selectImgIndex) {
            case 5:
              //身份证正面
              That.formData.identity_2 = responseData.data.img_url;
              break;
            case 6:
              //身份证反面
              That.formData.identity_1 = responseData.data.img_url;
              break;
            default:
              return;
            }
          } else {
            Toast(responseData.msg);
            Toast.clear();
          }
        })
        .catch(error => {
          Toast.clear();
          console.log(error);
        });
    },

    /**
     * 提交
     */
    submit: function() {
      const { type, name, mobile, province_id, city_id, district_id, street_id, address, license, identity_1, identity_2 } = this.formData;

      if (type == 1 && this.fun.isTextEmpty(name)) {
        Toast("请填写公司名称");
        return;
      }

      if (type == 2 && this.fun.isTextEmpty(name)) {
        Toast("请填写姓名");
        return;
      }

      if (this.fun.isTextEmpty(mobile)) {
        Toast("请填写联系电话");
        return;
      }

      if (this.fun.isMoblie(mobile)) {
        Toast("请输入正确的手机号");
        return;
      }

      if (this.fun.isTextEmpty(province_id) || 
          this.fun.isTextEmpty(city_id) || 
          this.fun.isTextEmpty(district_id)) {
        Toast("请选择省市区");
        return;
      }

      if (this.strShow && this.fun.isTextEmpty(street_id)) {
        Toast("请选择街道");
        return;
      }

      if (this.fun.isTextEmpty(address)) {
        Toast("请填写详细地址");
        return;
      }

      if (type == 1 && this.fun.isTextEmpty(license)) {
        Toast("请上传营业执照");
        return;
      }

      if (type == 2 && this.fun.isTextEmpty(identity_1)) {
        Toast("请上传身份证人像面");
        return;
      }

      if (type == 2 && this.fun.isTextEmpty(identity_2)) {
        Toast("请上传身份证国徽面");
        return;
      }

      let json = {
        category: this.applyType,
        operation_type: type,
        name,
        mobile,
        province_id,
        city_id,
        district_id,
        address
      };

      if (this.strShow) {
        json.street_id = street_id;
      }

      if (type == 1) {
        json.license = license;
      }

      if (type == 2) {
        json.identity_1 = identity_1;
        json.identity_2 = identity_2;
      }

      const that = this;

      $http.post("plugin.operation.frontend.index.apply-for", json, "加载中...")
        .then(function(response) {
          const { result, msg } = response;
          if (result == 1) {
            that.init();
            that.$dialog.alert({ message: msg || "申请成功" })
              .then(() => {
                that.$router.go(-1);
              });
          } else {
            Toast(msg || "申请失败");
          }
        });
    }
  }
};